﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;

namespace SharpDepend.WP8
{
    /// <summary>
    /// Reference: https://msdn.microsoft.com/en-us/library/windows/apps/jj714087(v=vs.105).aspx
    /// </summary>
    internal class FileManager : IFileManagerManager
    {
        // TODO: Fixa så att vi kan spara exporterat till mina dokument :)
        //string[] SpecialSplit = new string[] { ":/" };

        //IAsyncOperation<StorageFile> CreateSpecial(string path)
        //{
        //    IAsyncOperation<StorageFile> item;

        //    if (path.StartsWith(SpecialDrives.DocumentMusic))
        //    {
        //        string path2 = path.Split(SpecialSplit, System.StringSplitOptions.None)[1].Replace('/', '\\');
        //        item = KnownFolders.MusicLibrary.CreateFileAsync(path2);
        //    }
        //    else
        //    {
        //        string fixedPath = FixPath(path);

        //        // Create the local folder.
        //        item = ApplicationData.Current.LocalFolder.CreateFileAsync(fixedPath);
        //    }

        //    return item;
        //}

        public bool PathExist(string path, bool directory)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (directory)
                {
                    return local.DirectoryExists(path);
                }

                return local.FileExists(path);
            }
        }

        public void CreateFolder(string path)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                local.CreateDirectory(path);
            }
        }

        public Stream CreateFile(string path)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return local.CreateFile(path);
            }
        }

        public Stream OpenFile(string path)
        {
            // Get the local folder.
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return local.OpenFile(path, FileMode.Open);
            }
        }

        public string[] ListFiles(string folderPath)
        {
            // Get the local folder.
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string[] files = local.GetFileNames(folderPath);

                for (int i = 0; i < files.Length; i++)
                {
                    string file = files[i];

                    files[i] = folderPath + "/" + file;
                }

                return files;
            }
        }

        public void CopyFile(Stream stream, string destinationPath)
        {
            using (var streamDestination = CreateFile(destinationPath))
            {
                const int SIZE = 1024;
                byte[] buffer = new byte[SIZE];
                int read = SIZE;
                while (read == SIZE)
                {
                    read = stream.Read(buffer, 0, SIZE);
                    streamDestination.Write(buffer, 0, read);
                }
            }
        }
        
        public void MoveFile(string sourcePath, string destinationPath)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                local.MoveFile(sourcePath, destinationPath);
            }
        }

        public string[] ListFolders(string folderPath)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string[] subFolders = local.GetDirectoryNames(folderPath);

                for (int i = 0; i < subFolders.Length; i++)
                {
                    string folder = subFolders[i];

                    subFolders[i] = folderPath + "/" + folder + "/";
                }

                return subFolders;
            }
        }

        public void DeleteFile(string filePath)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                local.DeleteFile(filePath);
            }
        }

        public void DeleteFolder(string folderPath)
        {
            using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication())
            {
                local.DeleteDirectory(folderPath);
            }
        }

        public string[] GetDrives(DriveType driveType)
        {
            // TODO: Return Music:/ osv.
            return new string[0];
        }
    }
}
